<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/20
 * Time: 8:41
 */
namespace app\controller;


use app\model\doctorModel;
use vendor\core\view;

class Index
{
    //专家列表
    public function index(){
        $model = new doctorModel();
        $list = $model->select();
        view::fetch('index/index',[
            'list'=>$list
        ]);
    }
    //添加专家
    public function add(){
        $method = strtolower($_SERVER['REQUEST_METHOD']);
        if($method == 'get'){
            return view::fetch('index/add',[]);
        }
        //POST
        //执行真正添加操作
        $data = $_POST['model'];
        $data['create_time'] = time();
        $data['update_time'] = time();

        //处理图片
        $imagePath = $this->dealWithImage();
        if(empty($imagePath)){
            echo '没有上传照片: <a href="/index.php/index/add">重新上传</a><a href="/index.php/index/index">专家列表</a>';
            die;
        }
        $data['image'] =$imagePath;

        $model = new doctorModel();
        $result = $model->add($data);
        if($result['status']){
            echo '添加成功<a href="/index.php/index/add">继续添加</a><a href="/index.php/index/index">专家列表</a>';
        }else{
            echo '添加失败: '.$result["message"].'<a href="/index.php/index/add">重新添加</a><a href="/index.php/index/index">专家列表</a>';
        }

    }
    //编辑专家
    public function edit(){
        $method = strtolower($_SERVER['REQUEST_METHOD']);
        $id = $_GET['id'];
        if(empty($id)){
            echo '编辑失败：没有ID参数';die;
        }
        if($method == 'get'){
            $model = new doctorModel();
            $data = $model->select(null,'id='.$id);
            if(empty($data)){
                echo '编辑失败：没有相应记录';die;
            }
            $record = $data[0];
            return view::fetch('index/edit',[
                'record'=>$record
            ]);
        }
        //POST
        //执行真正添加操作
        $data = $_POST['model'];
        $data['update_time'] = time();

        //处理图片
        $imagePath = $this->dealWithImage();
        if(!empty($imagePath)){
            $data['image'] =$imagePath;
        }

        $model = new doctorModel();
        $result = $model->udpate($data,'id='.$id);
        if($result['status']){
            echo '编辑成功<a href="/index.php/index/index">专家列表</a>';
        }else{
            echo '编辑失败: '.$result["message"].'<a href="/index.php/index/index">专家列表</a>';
        }

    }
    //删除专家
    public function delete(){
        $ids = $_POST['doctor_ids'];
       //delete from doctor where id in (1,3,4)
        $model = new doctorModel();
        $ids_str = implode(',',$ids);
        $where = "id in ($ids_str)";
        $result = $model->delete($where);
        if($result['status']){
            echo '删除成功<a href="/index.php/index/index">继续删除</a><a href="/index.php/index/index">专家列表</a>';
        }else{
            echo '删除失败: '.$result["message"].'<a href="/index.php/index/index">重新失败</a><a href="/index.php/index/index">专家列表</a>';
        }
    }
    //处理图片
    private function dealWithImage(){
        //Array ( [file] => Array ( [name] => flower48.jpg [type] => image/jpeg
        // [tmp_name] => C:\Windows\php1DD1.tmp [error] => 0 [size] => 31701 ) )
        if(empty($_FILES['file']['name'])){
            return '';
        }
        $temp_path = $_FILES['file']['tmp_name'];
        $old_name = $_FILES['file']['name'];
        $old_name_arr = explode('.',$old_name);
        $newPath = md5($old_name.time()).'.'.$old_name_arr[1];
        move_uploaded_file($temp_path,APP_PATH.'/public/image/'.$newPath);
        return $newPath;
    }
    public function login(){
    view::fetch('index/login',[]);
    }
	private function login_(){
	$arr=[
	  'acc'=>$_POST['acc'],
        'pwd'=>$_POST['pwd']
    ];
	
	
	}
}